package com.asohofrucol.web;

import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;

import com.asohofrucol.dto.AgrarianResourceRegisterDTO;
import com.asohofrucol.dto.AsohofrucolSessionDTO;
import com.asohofrucol.service.UserOwnerService;

public class AgrarianBusinessController {
	@Autowired
	private AsohofrucolSessionDTO aSession;
	@Autowired
	private UserOwnerService userOwnerService;

	public void createNewRegister(ActionEvent actionEvent) {
		if (aSession.getAgrarianResource().getAgrarianResourceRegisters()
				.size() < 3) {
			Long ruatId = aSession.getSelectedRuatRegister().getId();
			AgrarianResourceRegisterDTO agrarianResourceRegisterDTO = new AgrarianResourceRegisterDTO();
			agrarianResourceRegisterDTO.setRuatId(ruatId);
			agrarianResourceRegisterDTO.setStatus("1");
			aSession.getAgrarianResource().getAgrarianResourceRegisters()
					.add(agrarianResourceRegisterDTO);
		}
	}
	public void deleteRegister(ActionEvent actionEvent) {
		AgrarianResourceRegisterDTO selected = (AgrarianResourceRegisterDTO) actionEvent
				.getComponent().getAttributes().get("register");
		aSession.getAgrarianResource().getAgrarianResourceRegisters()
				.remove(selected);
		if(selected.getId()!=null){
			String userModifier = aSession.getUserInfo().getUserName(); 
			userOwnerService.deleteAgrarianResourceRegister(selected, userModifier);
		}
	}
}
